package com.atguigu.day07;

import com.atguigu.utils.ProductViewCountPerWindow;
import com.atguigu.utils.UserBehavior;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Comparator;

public class Example2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .readTextFile("/home/zuoyuan/flinktutorial0212/src/main/resources/UserBehavior.csv")
                .flatMap(new FlatMapFunction<String, UserBehavior>() {
                    @Override
                    public void flatMap(String in, Collector<UserBehavior> out) throws Exception {
                        String[] array = in.split(",");
                        UserBehavior userBehavior = new UserBehavior(
                                array[0], array[1], array[2], array[3],
                                Long.parseLong(array[4]) * 1000L
                        );
                        if (userBehavior.type.equals("pv")) {
                            out.collect(userBehavior);
                        }
                    }
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<UserBehavior>forMonotonousTimestamps()
                        .withTimestampAssigner(new SerializableTimestampAssigner<UserBehavior>() {
                            @Override
                            public long extractTimestamp(UserBehavior element, long recordTimestamp) {
                                return element.ts;
                            }
                        })
                )
                .keyBy(r -> r.productId)
                .process(new MySlidingWindow(60 * 60 * 1000L, 5 * 60 * 1000L))
                .keyBy(r -> r.windowEndTime)
                .process(new KeyedProcessFunction<Long, ProductViewCountPerWindow, String>() {
                    private ListState<ProductViewCountPerWindow> listState;
                    @Override
                    public void open(Configuration parameters) throws Exception {
                        listState = getRuntimeContext().getListState(
                                new ListStateDescriptor<ProductViewCountPerWindow>(
                                        "list", Types.POJO(ProductViewCountPerWindow.class)
                                )
                        );
                    }

                    @Override
                    public void processElement(ProductViewCountPerWindow in, Context ctx, Collector<String> out) throws Exception {
                        listState.add(in);

                        ctx.timerService().registerEventTimeTimer(in.windowEndTime + 1L);
                    }

                    @Override
                    public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                        ArrayList<ProductViewCountPerWindow> arrayList = new ArrayList<>();
                        for (ProductViewCountPerWindow e : listState.get()) arrayList.add(e);
                        listState.clear();

                        arrayList.sort(new Comparator<ProductViewCountPerWindow>() {
                            @Override
                            public int compare(ProductViewCountPerWindow p1, ProductViewCountPerWindow p2) {
                                return (int)(p2.count - p1.count);
                            }
                        });

                        StringBuilder result = new StringBuilder();
                        result.append("=====================================\n")
                                .append("窗口结束时间：" + new Timestamp(timestamp - 1L) + "\n");
                        for (int i = 0; i < 3; i++) {
                            ProductViewCountPerWindow tmp = arrayList.get(i);

                            result.append("第" + (i+1) + "名的商品ID：" + tmp.productId + "，浏览次数：" + tmp.count + "\n");
                        }
                        result.append("=====================================\n");

                        out.collect(result.toString());
                    }
                })
                .print();

        env.execute();
    }

    public static class MySlidingWindow extends KeyedProcessFunction<String, UserBehavior, ProductViewCountPerWindow> {
        private long windowSize;
        private long windowStep;

        public MySlidingWindow(long windowSize, long windowStep) {
            this.windowSize = windowSize;
            this.windowStep = windowStep;
        }

        private MapState<Tuple2<Long, Long>, Long> mapState;

        @Override
        public void open(Configuration parameters) throws Exception {
            mapState = getRuntimeContext().getMapState(
                    new MapStateDescriptor<Tuple2<Long, Long>, Long>(
                            "windowinfo-accumulator",
                            Types.TUPLE(Types.LONG, Types.LONG),
                            Types.LONG
                    )
            );
        }

        @Override
        public void processElement(UserBehavior in, Context ctx, Collector<ProductViewCountPerWindow> out) throws Exception {
            long firstStart = in.ts - in.ts % windowStep;
            ArrayList<Tuple2<Long, Long>> windowInfoArray = new ArrayList<>();
            for (long start = firstStart; start > in.ts - windowSize; start -= windowStep) {
                windowInfoArray.add(Tuple2.of(start, start + windowSize));
            }

            for (Tuple2<Long, Long> windowInfo : windowInfoArray) {
                if (!mapState.contains(windowInfo)) {
                    mapState.put(windowInfo, 1L);
                } else {
                    mapState.put(windowInfo, mapState.get(windowInfo) + 1L);
                }

                ctx.timerService().registerEventTimeTimer(
                        windowInfo.f1 - 1L
                );
            }
        }

        @Override
        public void onTimer(long timestamp, OnTimerContext ctx, Collector<ProductViewCountPerWindow> out) throws Exception {
            long windowEndTime = timestamp + 1L;
            long windowStartTime = windowEndTime - windowSize;
            Tuple2<Long, Long> windowInfo = Tuple2.of(windowStartTime, windowEndTime);
            long count = mapState.get(windowInfo);
            String productId = ctx.getCurrentKey();
            out.collect(new ProductViewCountPerWindow(
                    productId, count, windowStartTime, windowEndTime
            ));

            mapState.remove(windowInfo);
        }
    }
}
